<template>
	<view class="content">
		<view class="accept-box">
			<image class="inv-top" src="@/static/img/invite-top.png" mode="widthFix"></image>
			<view class="inv-con">
				<view class="item-phone">您的好友{{ mobile }}为您推荐法力</view>
				<view class="inv-bot-ipt-box">
<!--					<input type="number" v-model="acceptMobile" maxlength="11" placeholder="请输入您的手机号码" placeholder-style="font-size:24rpx" />-->
					<button  class="u-reset-button item-txt"
					        open-type="getPhoneNumber" @getphonenumber="thirdLogin">
						<view class="text u-flex u-row-center u-col-center" style="color:#fff;width: 100%;height: 100%;">我要领券</view>
					</button>
				</view>
				<view class="coupon-new"><image src="@/static/img/coupon-new.png" mode="widthFix"></image></view>
			</view>
			<image class="inv-top" src="@/static/img/invite-bot.png" mode="widthFix"></image>
		</view>
		<uni-popup ref="popupFindLawery" type="center" :border-radius="20" @close="close" @open="open" :closeable="true">
			<view style="padding: 20rpx;width: 80vw;height: 100vw;background: #fff;border-radius: 20rpx;">
				<image :src="downloadData.qrpath" style="width: 100%;height: 100%;" :show-menu-by-longpress='true'></image>
			</view>
		</uni-popup>
	</view>
</template>

<script>
import wx from 'weixin-js-sdk';
import wechat from "@/common/wechat/wechat";
import router from "@/common/router";
import api from "@/common/request";
export default {
	data() {
		return {
			inviteCode: '',
			mobile: '',
			acceptMobile: '',
			time: '',
			loginRes: {},
			isShow:false,
			downloadData:{},
			brand:""
		};
	},
	onLoad(options) {
		this.brand = this.judgeBrand(uni.getSystemInfoSync().brand);
		console.log('手机型号:', this.brand);
		//console.log("手机牌子" + this.brand);
		let routes = getCurrentPages();
		let curParam = routes[routes.length - 1].options;
		this.inviteCode = curParam.invite;
		if(this.$Route.query.id){
			console.log(this.$Route.query.id)
			uni.setStorageSync('pid', this.$Route.query.id);
		}
		this.init();

	},

	methods: {
		saveImage(){
			let that = this;
			uni.showModal({
			
			})
		},
		// 路由跳转
		jump(path, parmas) {
			this.$Router.push({
				path: path,
				query: parmas
			});
		},
	    ApplicationForRegistration(){
	      setTimeout(() => {
	       location.href = 'https://d-falih5.bigchun.com/key_register/key-register.html';
	      }, 500);
	    },
		downFlzx() {
			// 点击确认按钮点击事件
			if (this.brand == "iphone") {
				let appStoreScheme = "itms-apps://itunes.apple.com/app/id1462724492"
				location.href = appStoreScheme;
				plus.runtime.openURL(appStoreScheme, (res) => {
					uni.showToast({
						title: "AppStore打开失败，请自行前往下载。",
						icon: "none"
					})
				});
			} else {
				if (this.brand == "xiaomi" || this.brand == "mi" || this.brand == "redmi" || this.brand == "mix") {
					location.href = 'mimarket://details?id=com.lifakeji.lark.business.law'
					return;
				}
				if (this.brand == "huawei") {
					location.href = 'appmarket://details?id=com.lifakeji.lark.business.law'
					return;
				}
				if (this.brand == "oppo") {
					location.href = 'oppomarket://details?id=com.lifakeji.lark.business.law'
					return;
				}
				if (this.brand == "vivo") {
					location.href = 'vivomarket://details?id=com.lifakeji.lark.business.law'
					return;
				}
				// 安卓直接打开应用宝的URL
				plus.runtime.openURL("https://a.app.qq.com/o/simple.jsp?pkgname=com.lifakeji.lark.business.law");
			}
		},
		async init() {
			uni.login({
				provider: 'weixin',
				success: res => {
					this.loginRes = res;
				},
				fail: err => {
					uni.showToast({
						title: err.msg,
						icon: 'none'
					});
				}
			});
			// wx配置
			let res = await this.$api('index.share');
			let sign = res.data;
			wx.config({
				debug: false,
				appId: sign.appId,
				timestamp: sign.timestamp,
				nonceStr: sign.nonceStr,
				signature: sign.signature,
				jsApiList: sign.jsApiList
			});
			wx.ready(() => {
				// 朋友圈
				wx.updateTimelineShareData({
					title: '快来领取1000元新人红包，限时大放送！！！', // 标题
					link: `${this.$VIEW_URL}/#/pages/client/user/accept-invite?invite=${this.inviteCode}`, //分享链接
					imgUrl: `${this.$API_URL}/source/go.png` // 分享图标
				});
				// 发送给朋友
				wx.updateAppMessageShareData({
					title: '快来领取1000元新人红包，限时大放送！！！', // 标题
					desc: '找律师，上法力。0元下单，不满意不付钱，败诉退款，无理由更换律师，主打就是让你爽。', // 分享描述
					link: `${this.$VIEW_URL}/#/pages/client/user/accept-invite?invite=${this.inviteCode}`, //分享链接
					imgUrl: `${this.$API_URL}/source/go.png` // 分享图标
				});
			});
		},
		// 第三方登录
		thirdLogin(payload) {
			let that = this;
			// uni.showLoading({
			// 	title: that.eventMap('login')
			// });
			console.log("====payload:",payload)
			if (payload.detail.errMsg !== 'getPhoneNumber:ok') {
				uni.showToast({
					title: payload.detail.errMsg,
					icon : "none"
				});
				return Promise.reject(payload.detail.errMsg);
			}
			console.log("===payload", payload.detail);
			that.$api("index.add_reg", {
				code         : that.loginRes.code,
				iv           : payload.detail.iv,
				encryptedData: payload.detail.encryptedData,
				pid          : uni.getStorageSync("pid")
			}).then(res=>{
				console.log("res====",res)
				if (res.code === 1) {
					that.$refs.popupFindLawery.open();
					that.downloadData = res.data;
					console.log("res====",that.downloadData)
				} else {
					uni.showToast({
						title: "获取手机号失败",
						icon: 'none'
					});
				}
			});
		},
		close(){
			this.isShow = false;
		},
		open(){
			this.isShow = true;
		},
		eventMap(event) {
			let map = "";
			switch (event) {
				case "login":
					map = "获取中...";
					break;
				case "refresh":
					map = "更新中...";
					break;
				case "bind":
					map = "绑定中...";
					break;
			}
			return map;
		},
		judgeBrand(sUserAgent) {
			var isIphone = sUserAgent.match(/Apple/i) == "iphone";
			var isHuawei = sUserAgent.match(/Huawei/i) == "huawei";
			var isHonor = sUserAgent.match(/Honor/i) == "honor";
			var isOppo = sUserAgent.match(/OPPO/i) == "oppo";
			var isOppoR15 = sUserAgent.match(/pacm00/i) == "pacm00";
			var isVivo = sUserAgent.match(/Vivo/i) == "vivo";
			var isXiaomi = sUserAgent.match(/Xiaomi\s/i) == "mi ";
			var isXiaomi2s = sUserAgent.match(/Mix\s/i) == "mix ";
			var isRedmi = sUserAgent.match(/Redmi/i) == "redmi";
			var isSamsung = sUserAgent.match(/Samsung-/i) == "sm-";
			
			if (isIphone) {
				return 'iphone';
			} else if (isHuawei || isHonor) {
				return 'huawei';
			} else if (isOppo || isOppoR15) {
				return 'oppo';
			} else if (isVivo) {
				return 'vivo';
			} else if (isXiaomi || isRedmi || isXiaomi2s) {
				return 'xiaomi';
			} else if (isSamsung) {
				return 'samsung';
			} else {
				return 'default';
			}
		}
	}
};
</script>

<style lang="scss">
.content {
	background-color: #52886f;
	padding-top: 140rpx;
}
.accept-box {
	width: 100%;
	height: 100%;
	.inv-top {
		width: 100%;
	}
	.inv-con {
		padding: 40rpx 80rpx;
		box-sizing: border-box;
		.item-phone {
			color: #fff;
			font-size: 30rpx;
			text-align: center;
			font-weight: bold;
			text-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.4);
			margin-bottom: 30rpx;
		}
		.inv-bot-ipt-box {
			box-sizing: border-box;
			position: relative;
			height: 70rpx;
			display: flex;
			border-radius: 10rpx;
			margin-bottom: 30rpx;
			.item-txt {
				width: 100%;
				height: 70rpx;
				line-height: 60rpx;
				box-sizing: border-box;
				position: absolute;
				top: -6rpx;
				padding: 0 20rpx;
				border: 6rpx solid #0d0409;
				border-radius: 10rpx;
				color: #ffffff;
				text-align: center;
				background: linear-gradient(to right, #9730E9, #5100B8);
			}
			input {
				font-size: 28rpx;
				padding: 10rpx;
			}
		}
	}
}
.coupon-new{
			width: 400rpx;
			margin: 0 auto 10rpx;
			image{
				width: 100%;
				height: 100%;
			}
		}
</style>
